package com.suxin.algorithm.array;

/**
 * @author Tang
 * @classname ArrayTest
 * @description [ 原生数组 ]
 * @date 2022/3/21 22:44
 */
public class ArrayTest {

    public int size;
    public int[] data;
    public int index;

    public ArrayTest(int size) {
        this.size = size;
        this.index = 0;
        this.data = new int[size];
    }

    /**
     * 要插入位置的元素后移O(n)
     * @param loc
     * @param n
     */
    public void insert(int loc, int n) {
        for (int i = size - 1; i > loc; i--) {
            // 元素后移
            data[i + 1] = data[i];
        }
        data[loc] = n;
    }

    /**
     * O(n)
     * @param loc
     */
    public void delete(int loc) {
        for (int i = loc; i < size; i++) {
            // 元素前移
            data[i] = data[i + 1];
        }
    }

    public void update(int m, int loc) {
        data[loc] = m;
    }

    public int get(int loc) {
        return data[loc];
    }

}